package com.android.hedgehog;

import java.util.Random;

import android.graphics.Color;

import com.android.hedgehog.framework.Game;

public class Bot0 extends BotScreen {

	Random rand = new Random();

	int sprite; // current sprite number to display
	double spriteTime; // totalTime for a sprite to switch
	int totalSprites = 4; // totalNumber of sprites for regular movement
	double standByTime;
	int calculateTimeLimit = 12;
	int[] point = { 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5 };
	double answerTime;
	double accuracy;
	double speed;
	int totalAccuracy;
	int questionPos = 200;
	int totalHits = 0;
	int totalMisses = 0;
	boolean blue = false;
	boolean directHit;
	double questionBuffer;
	public String test = "";

	public Bot0(Game game) {
		name = "LOGIC-BOT";
		graphics = game.getGraphics();
		sprite = 0;
		spriteTime = 0.0;
		accuracy = 0;
		answerTime = 0;
		score = 0;
		speed = 5;
		phase = Phase.StandBy;

	}

	public void update(float deltaTime) {
		if (phase == Phase.StandBy)
			updateStandBy(deltaTime);
		if (phase == Phase.Calculate)
			updateCalculate(deltaTime);
		if (phase == Phase.Answer)
			updateAnswer();

	}

	private void updateStandBy(float deltaTime) {
		if (calculating) {
			phase = Phase.Calculate;
			return;
		}
		standByTime += deltaTime;
		if (standByTime > spriteTime) {
			spriteTime += .50;
			if (sprite == (totalSprites - 1))
				sprite = 0;
			else
				sprite++;
		}
	}

	private void updateCalculate(float deltaTime) {
		answerTime += deltaTime;
		accuracy = answerTime * speed;
		accuracy -= (totalHits * 10);
		accuracy += (totalMisses * 3);

		if (answerTime > questionBuffer) {
			directHit = false;
			questionBuffer += .6;
			questionPos = 180 + rand.nextInt(105);

			if (rand.nextInt(4) == 0)
				blue = true;
			else
				blue = false;

		}

		if (answerTime > calculateTimeLimit || accuracy >= 100) {
			phase = Phase.Answer;
			answerTime = 0;
			questionBuffer = 0;
			totalHits = 0;
			totalMisses = 0;
		}
	}

	private void updateAnswer() {
		if (accuracy > 100)
			accuracy = 100;
		if (accuracy < 0)
			accuracy = 0;

		totalAccuracy = (int) Math.round(100 * ((accuracy + 20) / ((double) 120)));
		if (totalAccuracy > (rand.nextInt(100) + 1)) {
			correct = true;
			score += selected;
		} else {
			correct = false;
		}
		phase = Phase.StandBy;
		calculating = false;
	}

	public void present() {

		if (phase == Phase.StandBy)
			presentStandBy();
		if (phase == Phase.Calculate)
			presentCalculate();
		if (phase == Phase.Answer)
			presentAnswer();
	}

	private void presentStandBy() {
		graphics.drawPixmap(Assets.bottest, 220, 135, 0 + (80 * sprite), 120, 80, 80);
		// graphics.drawText("" + totalAccuracy, 230, 50, 12, Color.BLACK); //
		// test
	}

	private void presentCalculate() {
		if (!directHit) {
			graphics.drawPixmap(Assets.bottest, 220, 135, 0, 120, 80, 80);
		} else {
			graphics.drawPixmap(Assets.bottest, 220, 135, 720, 120, 96, 80);
		}
		if (blue) {
			graphics.drawUniversalText("?", 2, true, questionPos, 135, 48, 255, Color.BLUE);
		} else {
			graphics.drawUniversalText("?", 2, true, questionPos, 135, 48, 255, Color.GREEN);
		}

		for (int i = 0; i < accuracy; i++) {
			graphics.drawLine(70 + i, 63, 70 + i, 78, Color.RED);
			// graphics.drawText("" + accuracy, 230, 70, 12, Color.BLACK); //
			// test
		}
		// graphics.drawText(test+" H:"+totalHits+" M:"+totalMisses, 230, 85,
		// 12, Color.BLACK); // test
		graphics.drawUniversalRect(70, 63, 100, 15, false, false, 3, 255, Color.BLACK);
	}

	private void presentAnswer() {
		graphics.drawPixmap(Assets.bot0, 220, 135, 0 + (70 * sprite), 0, 70, 110);
	}

	public void select() {
		selected = point[rand.nextInt(point.length)];
		speed = 7;
		speed += (3 - selected);
	}

	public void ifHit(int x, int y) {
		if ((x > 220 && x < 290 && y > 135 && y < 245 && blue == false)) {
			test = "miss";
			totalMisses++;
		}

		if (x > 220 && x < 290 && y > 135 && y < 245 && blue) {
			totalHits++;
			directHit = true;
			blue = false; //protects repeats
			test = "hit";
		}

	}

}
